/**
 * Created with IntelliJ IDEA.
 * User: teddy zhang
 * Date: 2015-04-17
 * Time: 11:25
 */

define(function (require, exports, module) {
    'use strict';
    
    var rk = require('rk');

    $.widget('rk.usersettingSubscribeCtrl', {
        options: {},

        _create: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._render();
            me._bind();

        },

        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;

        },

        _render: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var subscribe_HTML = require('page/tmpl/usersetting/tpl_user_setting_subscribe.tpl');
            $.postJson('/json/oa_profile-edit/subscribe.action').done(function (json) {
                if (json.status == 0) {
                    var data = json.data;
                    $(elem).html(rk.templateText(subscribe_HTML, data));
                    $(elem).find(".nc_fixtime,.nc_realtime").buttonset();
                }
            });

        },


        _bind: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            $(elem).on('click', 'a[act="subscribe_btn_save"]', function (event) {
                event.preventDefault();

                var fixParamJsonArray =[];
                var realParamJsonArray =[];

                $(elem).find('li.nc_fixtime input').each(function(index,item){
                         fixParamJsonArray.push({id:$(item).attr('data-id'),status:($(item).attr('checked')?'1':'0')});
                });

                $(elem).find('li.nc_realtime input').each(function(index,item){
                    realParamJsonArray.push({id:$(item).attr('data-id'),status:($(item).attr('checked')?'1':'0')});
                });

                var postData = {fixParamJsonArray:JSON.stringify(fixParamJsonArray),realParamJsonArray:JSON.stringify(realParamJsonArray)};
                $.postJson('/json/oa_profile-edit/subscribe-update.action',postData).done(function (json) {
                    if (json.status == 0) {
                        var data = json.data;
                        rk.noticeSuccess(rk.i18n('USERSET_SUB_SAVED'));
                    }else{
                        rk.noticeError(rk.i18n('USERSET_SERVER_ERR'));
                    }
                });
                return false;
            });

        }

    });

    return $;

});

